//
//  PKGroupMainViewController.m
//  PianKe
//
//  Created by mingtao on 15/9/25.
//  Copyright (c) 2015年 mingtao. All rights reserved.
//

#import "PKGroupMainViewController.h"
#import "PKRequestManager.h"
#import "PKGroupModel.h"
#import "PKGroupTableViewCell.h"
#import "PKGroupImageTableViewCell.h"
#import "PKGrouplistModel.h"
#import "PKGrouplistModel.h"

@interface PKGroupMainViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)NSMutableArray *NsArray;

@property(nonatomic,strong)PKGroupModel *groupModel;
@property(nonatomic,strong)UITableView *maintableview;

@property(nonatomic,strong)PKGrouplistModel *listmo;



@end


@implementation PKGroupMainViewController

-(NSMutableArray *)NsArray
{
    if (_NsArray==nil) {
        _NsArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _NsArray;

}

-(void)request
{
    NSMutableDictionary *dicgroup=[NSMutableDictionary dictionary];
   // if ([_stop isEqualToString:@"hot"]) {
         dicgroup[@"sort"]=self.stop;
    //}else{
    //   dicgroup[@"sort"]=@"addtime";
  //  }

   [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/group/posts_hotlist" parDic:dicgroup finish:^(NSData *data) {
       NSDictionary *didata=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
       
       NSArray *array=didata[@"data"][@"list"];
       [self.NsArray removeAllObjects];
       
       for (NSDictionary *keyDic in array) {
           _listmo = [[PKGrouplistModel alloc]init];
           
           [_listmo setValuesForKeysWithDictionary:keyDic];
           
           [self.NsArray addObject:_listmo];
       }
       
       
       [_maintableview reloadData];
       
   } error:^(NSError *error) {
       
       NSLog(@"PKGroupMainViewController数据请求异常%@",error);
       
   }];


}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    /*
     
     //创建一个分段控件，采用独有的初始化方法
     UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"陈曦",@"鲍坤",@"朱钊",@"高蒙",@"陈雷"]];
     //
     seg.frame = CGRectMake(10, 60, 355, 40);
     //
     //    seg.tag = 1001;
     //
     seg.tintColor = [UIColor redColor];//设置分段控件的主题颜色
     //
     seg.selectedSegmentIndex = 2;//指定选中的是第几个块
     
     */
    self.stop = @"addtime";
    self.titleLable.text = @"话题";
    self.view.backgroundColor =[UIColor whiteColor];
    UISegmentedControl *sec=[[UISegmentedControl alloc]initWithItems:@[@"Hot",@"New"]];
    sec.frame = CGRectMake(100, 70, 100, 30);
    
    sec.tintColor=[UIColor colorWithRed:0.716 green:1.000 blue:0.301 alpha:1.000];
    
    sec.selectedSegmentIndex=0;
    
    [sec addTarget:self action:@selector(segmentedconteolbut:) forControlEvents:UIControlEventValueChanged];
    
    
        [self.view addSubview:sec];
    
    _maintableview = [[UITableView alloc]initWithFrame:CGRectMake(0,100, WIHES, HHIHEI-70)];
    
    
    _maintableview.dataSource = self;
    _maintableview.delegate = self;
    
    // _maintableview.rowHeight=200;
   
    [self.view addSubview:_maintableview];
    
    
    [self request];
 
    
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.NsArray.count;

}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    PKGrouplistModel *groulist=self.NsArray[indexPath.row];
    if (![groulist.coverimg isEqualToString:@""]) {
             static NSString *strfilepaths=@"ss";
        PKGroupTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:strfilepaths];

        
        if (cell==nil) {
            //            gricell = [[[GirCell alloc]initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:strfilepaths]autorelease];
            cell=[[PKGroupTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strfilepaths];
        }
        
        cell.GroupModel = self.NsArray[indexPath.row];
        
        return cell;
        
    }else
    {
        static NSString *str=@"mm";
        
        PKGroupImageTableViewCell *cell1=[tableView dequeueReusableCellWithIdentifier:str];
        if (cell1==nil) {
            cell1=[[PKGroupImageTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str];
        }
        cell1.GroupModel1=self.NsArray[indexPath.row];
        
        return cell1;
    
    }
    
    
    
    
    
    

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKGrouplistModel *conta=self.NsArray[indexPath.row];
    if (![conta.coverimg isEqualToString:@""]) {
        return [PKGroupTableViewCell heigHtforContact1:conta];
        
    }else
    {
        return [PKGroupImageTableViewCell heigHtforContact1:conta];
        
    }
    
}
















- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
//UISegmentedControl
-(void)segmentedconteolbut:(UISegmentedControl *)sect
{
    switch (sect.selectedSegmentIndex) {
        case 0:
            
            _stop = @"addtime";
            [self request];
            
            break;
            
            case 1:
            
            _stop=@"hot";
            [self request];
        default:
            break;
    }

}

@end
